/**
 * Created by Administrator on 2017/3/27 0027.
 */
import React from "react";
import {Select} from 'antd';
const Option = Select.Option;
export default class Createorder_DetailItem extends React.Component{
    constructor(props){
        super(props);
        this.state={
            islast:false,
            isfrist:false,
            show:this.props.isshow,
            context:this.props.context,
        }
        this.context_price = '';
        this.context_quantity ='';
    }

    componentWillMount(){
        let totallenght = this.props.totallenght
        if(totallenght == 1){
            this.setState({
                isfrist:true,
                islast:true,
            });
            return;
        }

        if(((this.props.index+1) == totallenght)){
            this.setState({
                islast:true,
            });
        }

        // if(this.props.index == 0){
        //     this.setState({
        //         isfrist:true,
        //     });
        // }

    }

    changenam(contextname,e){
        let text = e.target.value;
        let rule = /^[0-9,'.']*$/;
        let rulenum = /^[0-9]*$/;
        let context = this.state.context;
        if(contextname=='remark') {
          if (text.length > 500) {
            return;
          }
        } else {
          if (text.length > 100) {
            return;
          }
        }
        if(contextname=='price'){
            if(!rule.test(text)){
                context[contextname] = this.context_price;

            }else{
                this.context_price = text;
                context[contextname] = this.context_price;
            }
        }else if(contextname=='quantity'){
            if(!rule.test(text)){
                context[contextname] = this.context_quantity;
            }else{
                this.context_quantity = text;
                context[contextname] = this.context_quantity;
            }

        }else{
            context[contextname] = text;
        }
        this.setState({
            context:context
        },()=>{
            this.props.setcountcallback();
        });
    }

    saveitem(){
        // let context = this.props.context
        // context[field] = this.state.name
        // this.props.repalceitem(this.state.context,this.props.index);
        // return false;
    }

    additem(){
        this.props.additemlist();
    }

    delteitem(index){
        this.props.delteitem(index);
    }

    addimg(){
        this.props.showimgmodal(this.props.index);
    }



    render(){
        let context = this.props.context;
        if(this.state.show){
            let totalmoney = this.state.context.price*this.state.context.quantity;
            if(isNaN(totalmoney)){
                totalmoney=''
            }else{
                totalmoney = Number.parseFloat(totalmoney).toFixed(2);
            }
            return (
                <thead>
                <tr className="order_from_trcontext">
                    <td className="order_frorm_td order_from_td_num" >{this.props.index+1}</td>
                    <td className="order_frorm_td order_from_td_snum"><input className="order_from_inputcontext" value={this.state.context.styleNo} onChange={this.changenam.bind(this,'styleNo')} /></td>
                    <td className="order_frorm_td order_from_td_cnum"><input className="order_from_inputcontext" value={this.state.context.productName} onChange={this.changenam.bind(this,'productName')}/></td>
                    <td className="order_frorm_td order_from_td_describe"><input className="order_from_inputcontext" value={this.state.context.description} onChange={this.changenam.bind(this,'description')}/></td>

                    <td className="order_frorm_td order_from_td_snum"><input className="order_from_inputcontext" value={this.state.context.yarnTypes} onChange={this.changenam.bind(this,'yarnTypes')}/></td>
                    <td className="order_frorm_td order_from_td_snum"><input className="order_from_inputcontext" value={this.state.context.yarnState} onChange={this.changenam.bind(this,'yarnState')}/></td>
                    <td className="order_frorm_td order_from_td_describe"><input className="order_from_inputcontext" value={this.state.context.constituent} onChange={this.changenam.bind(this,'constituent')}/></td>
                    <td className="order_frorm_td order_from_td_color"><input className="order_from_inputcontext" value={this.state.context.yarn} onChange={this.changenam.bind(this,'yarn')}/></td>
                    
                    
                    <td className="order_frorm_td order_from_td_monad">
                      <input className="order_from_inputcontext" value={this.state.context.prickle} onChange={this.changenam.bind(this, 'prickle')}/>
                    </td>

                    <td className="order_frorm_td order_from_td_pnum"><input className="order_from_inputcontext" value={this.state.context.quantity} onChange={this.changenam.bind(this,'quantity')}/></td>
                    <td className="order_frorm_td order_from_td_ucost"><input className="order_from_inputcontext" value={this.state.context.price} onChange={this.changenam.bind(this,'price')}/></td>
                    

                    <td className="order_frorm_td order_from_td_moeny">{totalmoney}</td>
                    
                    
                    <td className="order_frorm_td order_from_td_moeny"><input className="order_from_inputcontext" value={this.state.context.desp} onChange={this.changenam.bind(this,'desp')}/></td>
                    <td className="order_frorm_td order_from_td_delandadd" >
                        <span className={this.state.islast?'order_from_sp_add':'order_from_sp_addmiss'} onClick={this.additem.bind(this)}></span>
                        <span className={this.state.isfrist?"order_from_sp_minusmiss":"order_from_sp_minus"} onClick={this.delteitem.bind(this,this.props.index)}></span>
                    </td>
                </tr>
                </thead>
            )
        }else{
            return (
                null
            )
        }

    }
}
